# First install Ansible and run the launcher-env-setup-playbook.
#
# From the same dir, run:
# >>  ansible-playbook launcher-playbook.yaml -vv
#
# To run the libvirt option (available for non-OVB jobs):
# Clone https://github.com/openstack/tripleo-quickstart and run:
# >> ansible-playbook launcher-playbook.yaml \
#    -e nodepool_provider=libvirt \
#    -e mirror_path=mirror.mtl01.inap.opendev.org
#
# To split mirror_path into different values for packages and pypi, use f.e.:
#    ...
#    -e mirror_fqdn=mirror.mtl01.inap.opendev.org \
#    -e package_mirror=http://mirror.centos.org/centos
#    -e pypi_fqdn=mirror.mtl02.inap.opendev.org \
#
# To run on a different cloud defined in clouds.yaml ( default is rdo-cloud):
# >> ansible-playbook launcher-playbook.yaml \
#    -e cloud_name='<custom cloud>'
#
# To run with a different clouds.yaml
# >> ansible-playbook launcher-playbook.yaml \
#    -e clouds_yaml_path='<full path to yaml file>'
#
# To force the job into failing status in order to hold the nodes (temporary option):
# >> ansible-playbook launcher-playbook.yaml \
#    -e force_post_failure=true
#
# If your upstream gerrit user or rdo gerit user (and/or keys) are different
# from your current $USER, there are options to set for each of those values.
# See the reproducer-zuul-based-quickstart.sh for the options details.

{% raw %}
- hosts: localhost
  tasks:
    - name: Add the localhost to the inventory
      add_host:
        name: "localhost"
        groups: "localhost"
        ansible_host: 127.0.0.1
        ansible_connection: local

    - name: Add the primary to the inventory
      add_host:
        name: "localhost"
        groups: "primary"
        ansible_fqdn: "localhost"
        ansible_user: "{{ lookup('env', 'USER') }}"
        ansible_private_key_file: "/home/{{ ansible_user }}/.ssh/{{ user_pri_key | default('id_rsa') }}"
        ansible_host: "localhost"
{% endraw %}

- import_playbook: playbooks/tripleo-ci-reproducer/pre.yaml

- hosts: localhost
  vars:
    {% raw -%}
    clouds_yaml_path: "/home/{{ ansible_user }}/.config/openstack/clouds.yaml"
    {% endraw -%}
    ovb_key_name: tripleo-ci-team
    mirror_path: "{{ mirror_path }}"
    zuul_site_mirror_fqdn: "{{ zuul_site_mirror_fqdn | default(mirror_path) }}"
    mirror_fqdn: "{{ mirror_fqdn | default(mirror_path) }}"
    pypi_fqdn: "{{ pypi_fqdn | default(mirror_path) }}"
    {% if package_mirror is defined -%}
    package_mirror: "{{ package_mirror }}"
    {% endif -%}
    launch_job_branch: {{ zuul.branch }}
    {% if 'periodic' not in zuul.job -%}
    depends_on:
      - {{ zuul.change_url }}
    {% endif %}

    zuul_yaml: >-
      - project:
          check:
            jobs:
              - {{ zuul.job }}-dlrn-hash-tag

      - job:
          name: {{ zuul.job }}-dlrn-hash-tag
          parent: {{ zuul.job }}
          vars:
            {% if 'ovb' in zuul.job -%}
            {% raw -%}
            cloud_secrets:
              rdocloud:
                {{ lookup('file', '{{ clouds_yaml_path }}') | regex_search('username: (.+)') }}
                {{ lookup('file', '{{ clouds_yaml_path }}') | regex_search('password: (.+)') }}
                {{ lookup('file', '{{ clouds_yaml_path }}') | regex_search('project_name: (.+)') }}
                auth_url: https://phx2.cloud.rdoproject.org:13000/v3
                region_name: regionOne
                identity_api_version: 3
                user_domain_name: Default
                project_domain_name: Default
            key_name: {{ ovb_key_name }}
            {% endraw -%}
            cloud_settings:
              rdocloud:
                public_ip_net: 38.145.32.0/22
                undercloud_flavor: m1.large
                baremetal_flavor: m1.large
                bmc_flavor: m1.medium
                extra_node_flavor: m1.small
                baremetal_image: CentOS-7-x86_64-GenericCloud-1804_02
            remove_ovb_after_job: false
            {% endif -%}
            {% raw -%}
            zuul_site_mirror_fqdn: {{ zuul_site_mirror_fqdn }}
            mirror_fqdn: {{ mirror_fqdn }}
            pypi_fqdn: {{ pypi_fqdn }}
            package_mirror: {{ package_mirror }}
            {% endraw -%}
            {% if releases_file_output is defined -%}
            ready_releases_file: |
              {{ releases_file_output | replace('export', '  export') }}
            {% endif -%}
            {% if 'periodic' in zuul.job -%}
            force_periodic: true
            {% endif -%}
            registry_login_enabled: false
            featureset_override:
              {% if job.featureset_override is defined -%}
              {{ job.featureset_override | to_nice_yaml | indent(14) }}
              {% endif -%}
              {% if releases_file_output is not defined -%}
              {% if dlrn_hash is defined or ('undercloud' in hostvars and 'dlrn_hash' in hostvars['undercloud']) -%}
              dlrn_hash_tag: {{ hostvars['undercloud'].dlrn_hash }}
              {% endif -%}
              {% if dlrn_hash_newest is defined or ('undercloud' in hostvars and 'dlrn_hash_newest' in hostvars['undercloud']) -%}
              dlrn_hash_tag_newest: {{ hostvars['undercloud'].dlrn_hash_newest }}
              {%- endif %}
              {%- endif %}

  tasks:
    - import_role:
        name: ansible-role-tripleo-ci-reproducer
